package com.hsh.member.controller;


import com.hsh.member.base.Result;
import com.hsh.member.entity.Supplier;
import com.hsh.member.req.SupplierREQ;
import com.hsh.member.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 供应商信息表 前端控制器
 * </p>
 *
 * @author 海上花-www.hsh.com
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;


    /*
        localhost:6666/supplier/list/search/1/20
    *  分页条件查询供应商列表
    * */
    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") int page,
                         @PathVariable("size") int size,
                         @RequestBody(required = false) SupplierREQ req){

        return  supplierService.search(page, size, req);
    }

    /*
      新增供应商
     */
    //supplier
    @PostMapping
    public Result add(@RequestBody Supplier supplier){
        boolean b = supplierService.save(supplier);
        if(b){
            return  Result.ok();
        }
        else{
            return Result.error("新增供应商信息失败");
        }
    }

    /*
        localhost:6666/supplier/id
        通过供应商id删除数据
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id){
        return supplierService.deleteById(id);
    }

    /*
       locahosg:7777/supplier/id
       查询供应商详情
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") int id){
         Supplier supplier = supplierService.getById(id);
         return Result.ok(supplier);
    }

    @PutMapping("/{id}")
    public Result  update(@PathVariable("id") int id,
                          @RequestBody Supplier supplier){
       return supplierService.update(id,supplier);
    }
}
